﻿using System;

namespace NRex.Logging
{
    /// <summary>
    /// Outputs text to the Console.
    /// Colorizes Warn and Error writes.
    /// </summary>
    internal class ColoredConsoleLogger : ILogger
    {
        private static void ColoredWrite(string text, ConsoleColor foregroundColor)
        {
            var previousColor = Console.ForegroundColor;
            try
            {
                Console.ForegroundColor = foregroundColor;
                Console.WriteLine(text);
            }
            finally
            {
                Console.ForegroundColor = previousColor;
            }
        }

        public void Info(string text)
        {
            Console.WriteLine(text);
        }

        public void Info(string format, params object[] args)
        {
            Console.WriteLine(format, args);
        }

        public void Warn(string text)
        {
            ColoredWrite(text, ConsoleColor.Yellow);
        }

        public void Warn(string format, params object[] args)
        {
            ColoredWrite(string.Format(format, args), ConsoleColor.Yellow);
        }

        public void Error(string text)
        {
            ColoredWrite(text, ConsoleColor.Red);
        }

        public void Error(string format, params object[] args)
        {
            ColoredWrite(string.Format(format, args), ConsoleColor.Red);
        }
    }
}